package com.lin.linchatgpt.openai.server.imple;

import cn.dev33.satoken.stp.StpUtil;
import com.lin.linchatgpt.entity.Subject;
import com.lin.linchatgpt.entity.dto.MessageDTO;
import com.lin.linchatgpt.exception.LinException;
import com.lin.linchatgpt.openai.server.HistoryHandlerMessageStrategy;
import com.lin.linchatgpt.service.MessageService;
import com.lin.linchatgpt.service.SubjectService;
import com.lin.linchatgpt.utils.NanoMillisUtils;
import com.lin.linchatgpt.utils.ResultCode;
import com.unfbx.chatgpt.OpenAiClient;
import com.unfbx.chatgpt.entity.chat.ChatChoice;
import com.unfbx.chatgpt.entity.chat.ChatCompletion;
import com.unfbx.chatgpt.entity.chat.ChatCompletionResponse;
import com.unfbx.chatgpt.entity.chat.Message;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * Copyright(C),2022年-2022,霖霖
 *
 * @author 霖霖
 * @version 1.0
 * @date 2023/6/22 20:57
 * @Description 用于在新建主题时处理消息
 */
public class NewSubjectMessage implements HistoryHandlerMessageStrategy {
    private final OpenAiClient openAiClient;
    private final SubjectService subjectService;

    private final MessageService messageService;

    public NewSubjectMessage(OpenAiClient openAiClient, SubjectService subjectService, MessageService messageService) {
        this.openAiClient = openAiClient;
        this.subjectService = subjectService;
        this.messageService = messageService;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<com.lin.linchatgpt.entity.Message> historyMessageHandler(MessageDTO message) {

        Subject subject = new Subject();
        subject.setUserId(StpUtil.getLoginId().toString());
        // 总结提问内容
        StringBuilder stringBuilder = new StringBuilder();

        Message messageBuild = Message.builder().role(Message.Role.USER).content("请帮我根据下面这句话总结一个对话标题，标题长度不超过10个字。直接回复标题即可,不要有特殊字符。对话内容：" + message.getContent()).build();
        ChatCompletion chatCompletion = ChatCompletion.builder().messages(Collections.singletonList(messageBuild)).model(ChatCompletion.Model.GPT_3_5_TURBO.getName()).build();

        ChatCompletionResponse completionResponse;
        try {
            completionResponse = openAiClient.chatCompletion(chatCompletion);
        } catch (Exception e) {
            throw new LinException(ResultCode.OPENAI_SSE_ERROR, e.getMessage());
        }
        List<ChatChoice> choices = completionResponse.getChoices();
        for (ChatChoice choice : choices) {
            stringBuilder.append(choice.getMessage().getContent());
        }
        // 保存主题
        subject.setTitle(stringBuilder.toString());
        subject.setId(message.getSubjectId());


        subjectService.save(subject);

        com.lin.linchatgpt.entity.Message chatMessage = new com.lin.linchatgpt.entity.Message();
        BeanUtils.copyProperties(message,chatMessage);
        chatMessage.setRecordTime(NanoMillisUtils.getMillisTime());



        return Collections.singletonList(chatMessage);
    }
}
